package com.douqu.game.core.entity.ext.data.boon;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.DailySignRewardConfig;
import com.douqu.game.core.config.goods.PropConfig;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;

import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;

public class AutoRecover extends EntityObject{
    /**
     * 是否开启
     */
    public boolean enable;
    /**
     * 使用次数上限
     */
    public int maxUseTimes;
    /**
     * 百分比
     */
    public int percent;
    /**
     * 自动回血道具使用次数
     */
    public int autoUseTimes;
    /**
     * 战场道具使用次数
     */
    private Map<Integer,Integer> useTimes;

    /**
     * 是否使用军资类道具
     */
    private int militaryPropId;

    public void writeTo(ByteBuffer buffer){
        buffer.writeBoolean(enable);
        buffer.writeInt(maxUseTimes);
        buffer.writeInt(percent);
        buffer.writeInt(autoUseTimes);
        buffer.writeInt(militaryPropId);
    }

    public void loadForm(ByteBuffer buffer)
    {
        enable = buffer.readBoolean();
        maxUseTimes = buffer.readInt();
        percent = buffer.readInt();
        autoUseTimes = buffer.readInt();
        militaryPropId = buffer.readInt();
    }

    public boolean wasUseItem(){
        if(autoUseTimes > 0){
            return true;
        }

        for(int useTime : useTimes.values()){
            if(useTime > 0){
                return true;
            }
        }
        return false;
    }

    public AutoRecover() {
        this.militaryPropId = 0;
        this.enable = false;
        this.maxUseTimes = 1;
        this.percent = 1;
        useTimes = new HashMap<>();
        useTimes.put(ConstantFactory.PROP_TYPE_RECOVER, 0); // 恢复类道具使用次数
        useTimes.put(ConstantFactory.PROP_TYPE_REVIVE, 0); // 复活类道具使用次数
    }

    public void addAutoUseTime(){
        autoUseTimes++;
    }

    /**
     * 获取战场使用道具次数
     * @param propType
     * @return
     */
    public int getUseTimes(int propType){
        return useTimes.containsKey(propType) ? useTimes.get(propType) : 0;
    }

    /**
     * 增加战场使用道具次数
     * @param propType
     */
    public void addUseTimes(int propType){
        useTimes.merge(propType,1, (v1, v2) -> v1 + v2);
    }

    /**
     * 清除战场使用道具次数信息
     */
    public void clearItemTimes() {
        for(int type : useTimes.keySet()){
            useTimes.put(type,0);
        }
        this.autoUseTimes = 0;
        this.militaryPropId = 0;
    }

    public Map<Integer,Integer> getUseTimes(){
        return useTimes;
    }

    public int getMilitaryPropId() {
        return militaryPropId;
    }

    public void setMilitaryPropId(int militaryPropId) {
        if (this.militaryPropId == 0)
            this.militaryPropId = militaryPropId;
        else
        {
            PropConfig oldConfig = new PropConfig();
            oldConfig = DataFactory.getInstance().getGameObject(DataFactory.PROP_KEY,this.militaryPropId);
            PropConfig newConfig = new PropConfig();
            newConfig = DataFactory.getInstance().getGameObject(DataFactory.PROP_KEY,militaryPropId);
            if (newConfig.effectValue > oldConfig.effectValue)
                this.militaryPropId = militaryPropId;
        }
    }
}
